// pages/user/index.js
import { reLogin } from '../../service/request'
import userApi from '../../service/userApi'
import { $wuxLoading } from '../../components/wux/index'
const app = getApp()
let codeConfig = app.globalData.codeConfig
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hadAuthFlag: !!wx.getStorageInfoSync('hadAuthFlag'),
    userInfo: {},
    address: '',
    earningsInfo: {},
    showSharePopup: false,
    showCommissionRules: false, // 返佣规则弹窗
    showWithdrawMoney: false, // 提现说明弹窗
    picBaseUrl: app.globalData.picBaseUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.$wuxLoading = $wuxLoading()
    // 获取用户授权信息
    wx.getSetting({
      success: res => {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          this.setData({
            hadAuthFlag: true
          })
          this.getInitData()
        } else {
          this.setData({
            hadAuthFlag: false
          })
        }
      }
    })
  },
  goPage (e) {
    if (this.data.hadAuthFlag) {
      var url = e.currentTarget.dataset.url
      var talentId = e.currentTarget.dataset.talentid
      var actualEarnings = e.currentTarget.dataset.actualearnings
      var isTalent = e.currentTarget.dataset.istalent
      wx.navigateTo({
        url: `${url}?talentId=${talentId}&actualEarnings=${actualEarnings}&isTalent=${isTalent}`
      })
    }
  },
  onAboutOur(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  // 分享商品，跳转到用户达人店
  onShareGoods(e) {
    var url = e.currentTarget.dataset.url
    var talentId = e.currentTarget.dataset.talentid
    app.globalData.refreshIndexFlag = true
    wx.reLaunch({
      url: `${url}?talentId=${talentId}`   //注意switchTab只能跳转到带有tab的页面，不能跳转到不带tab的页面
    })
  },
  // 同意授权-并登陆
  bindGetUserInfo (e) {
    let that = this
    that.$wuxLoading.show({ text: '数据加载中' })
    // console.log(e.detail.userInfo)
    if (!!e.detail.userInfo) {
      wx.setStorageSync('hadAuthFlag', true)
      app.globalData.userInfo = e.detail.userInfo
      app.globalData.hadAuthUserInfo = true
      reLogin().then((res) => {
        that.$wuxLoading.hide()
        app.globalData.refreshIndexFlag = true
        wx.reLaunch({
          url: '../user/index'
        })
      })
    } else {
      that.$wuxLoading.hide()
      wx.setStorageSync('hadAuthFlag', false)
      this.setData({
        hadAuthFlag: false
      })
    }
  },
  getInitData () {
    var that = this
    userApi.getUserIndexData().then(res => {
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        that.setData({
          address: res.data.data.userInfo.address
        })
        res.data.data.userInfo.address = res.data.data.userInfo.address.substr(0, 10) + '.....' + res.data.data.userInfo.address.substr(-10, 10)
        that.setData({
          userInfo: res.data.data.userInfo,
          earningsInfo: res.data.data.earningsInfo
        })
      } else {
        // console.log(res)
      }
    })
  },
  // 显示分享的店铺海报
  handleShowQrCode () {
    let firstShared = wx.getStorageSync('firstShared')
    if (firstShared) {
      this.setData({
        showSharePopup: true
      })
    } else {
      this.setData({
        showCommissionRules: true
      })
    }
  },
  closeSharePopup () {
    this.setData({
      showSharePopup: false
      // showMask: false
    })
  },
  handleClearStorage () {
    wx.clearStorage({
      success: () => {
        wx.showToast({
          title: '缓存已清除',
          icon: 'none',
          duration: 1500
        })
      },
      fail: () => {
        wx.showToast({
          title: '缓存清除失败',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  // 关闭返佣规则
  closeCommissionRules () {
    this.setData({
      showCommissionRules: false
    })
    wx.setStorageSync('firstShared', true)
  },
  // 提现说明弹窗
  onWithdrawMoney () {
    this.setData({
      showWithdrawMoney: true
    })
  },
  // 关闭提现说明弹窗
  closeWithdrawMoney () {
    this.setData({
      showWithdrawMoney: false
    })
  },
  copyAddress (e) {
    let that = this
    wx.setClipboardData({
      data: that.data.address
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInitData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})